<?php
require_once APP_PATH.'models'.DS.'pendapatan'.DS.'PendapatanBL.php';
require_once APP_PATH.'models'.DS.'potongan'.DS.'PotonganBL.php';
require_once APP_PATH.'models'.DS.'divisi'.DS.'DivisiBL.php';
require_once APP_PATH.'models'.DS.'karyawan'.DS.'KaryawanBL.php';
require_once APP_PATH.'models'.DS.'gaji'.DS.'GajiBL.php';

class PayrollController extends AppController {

    public $name = 'Payroll';
    public $uses = array();
    
    // --- PENDAPATAN AREA
    public function pendapatanListDataPage() {
        $this->set('pendapatan', $this->paginate(ClassRegistry::init('Pendapatan')));

        ClassRegistry::flush();
    }

    public function pendapatanPembuatanPage() {
        $pendapatanBL = new PendapatanBL();
        $karyawanBL = new KaryawanBL();
        $divisiBL = new DivisiBL();

        $this->set('divisi_list', $divisiBL->divisiFind('list'));
        $this->set('karyawan_list', $karyawanBL->karyawanFind('list', array('conditions' => array('Karyawan.id NOT IN(SELECT karyawan_id FROM pendapatan)'))));

        if (!empty($this->data)) {
            if ($pendapatanBL->createPendapatan($this->data)) {
                $this->Session->setFlash(__('The pendapatan has been saved', true));
                $this->redirect(array('action' => 'pendapatanListDataPage'));
            } else {
                $this->Session->setFlash(__('The pendapatan has not been saved', true));
            }
        }
    }

    public function pendapatanEditPage($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid potongan', true));
            $this->redirect(array('action' => 'pendapatanListDataPage'));
        }

        $potonganBL = new PendapatanBL();

        if (!empty($this->data)) {
            if($potonganBL->editPendapatan($this->data, $id)){
                $this->Session->setFlash(__('The potongan has been saved', true));
                $this->redirect(array('action' => 'potonganListDataPage'));
            } else {
                $this->Session->setFlash(__('The potongan could not be saved. Please, try again.', true));
            }            
        }
        
        if (empty($this->data)) {
            $this->data = $potonganBL->readDetail($id);
        }
    }
    
    // --- END OF PENDAPATAN AREA
    
    
    // --- POTONGAN AREA
    public function potonganListDataPage() {
        $this->set('potongan', $this->paginate(ClassRegistry::init('Potongan')));

        ClassRegistry::flush();
    }

    public function potonganPembuatanPage() {
        $potonganBL = new PotonganBL();
        $karyawanBL = new KaryawanBL();
        $divisiBL = new DivisiBL();

        $this->set('divisi_list', $divisiBL->divisiFind('list'));
        $this->set('karyawan_list', $karyawanBL->karyawanFind('list', array('conditions' => array('Karyawan.id NOT IN(SELECT karyawan_id FROM potongan)'))));

        if (!empty($this->data)) {
            if ($potonganBL->createPotongan($this->data)) {
                $this->Session->setFlash(__('The potongan has been saved', true));
                $this->redirect(array('action' => 'potonganListDataPage'));
            } else {
                $this->Session->setFlash(__('The potongan has not been saved', true));
            }
        }
    }

    public function potonganEditPage($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid potongan', true));
            $this->redirect(array('action' => 'potonganListDataPage'));
        }

        $potonganBL = new PotonganBL();

        if (!empty($this->data)) {
            if($potonganBL->editPotongan($this->data, $id)){
                $this->Session->setFlash(__('The potongan has been saved', true));
                $this->redirect(array('action' => 'potonganListDataPage'));
            } else {
                $this->Session->setFlash(__('The potongan could not be saved. Please, try again.', true));
            }
        }

        if (empty($this->data)) {
            $this->data = $potonganBL->readDetail($id);
        }
    }
    
    // --- END POTONGAN AREA

    // --- try pendapatan & potongan
    public function pendapatanDanPotonganListDataPage() {
        $pendapatanDanPotongan = array();
        $karyawanBL = new KaryawanBL();
        $karyawan = new Karyawan();

//        pr('asdadasdasda');
        
        $pendapatanDanPotongan = $karyawan->query('select kar.nama, pend.gaji_pokok, pend.uang_makan,pend.uang_transport,pend.premi_hadir,pend.imk,pend.uang_shift,pend.uang_jabatan,pend.tunjangan_skill,pend.tunjangan_kesehatan,pend.tunjangan_insentif,pend.uang_cuti,
        pot.telat_1,pot.telat_2,pot.telat_3,pot.telat_4,pot.telat_5,pot.pulang_cepat_1,pot.pulang_cepat_2,pot.pulang_cepat_3
        from karyawan kar
        inner join pendapatan pend on pend.karyawan_id = kar.id
        inner join potongan pot on pot.karyawan_id = kar.id');

//        pr('asdadasdasda');
//        pr($pendapatanDanPotongan);
//        $this->set('potongan', $this->paginate(ClassRegistry::init('Potongan')));
//        $this->set('pendapatanDanPotongan', $this->paginate($pendapatanDanPotongan));


//        $this->paginate = array(
//            'joins' => array(
//                array('table'=> 'pendapatan',
//                'type' => 'INNER',
//                'alias' => 'pend',
//                'conditions' => array('pend.karyawan_id = Karyawan.id')),
//                array('table'=> 'potongan',
//                'type' => 'INNER',
//                'alias' => 'pot',
//                'conditions' => array('pot.karyawan_id = Karyawan.id'))
//                ),
//            );
//        $this->Member->recursive = -1;
//        //$this->paginate['conditions'][]['Reservation.pagamento_verificato'] = 'SI';
//        $this->paginate['fields'] = array('Karyawan.nama, pend.gaji_pokok, pend.uang_makan,pend.uang_transport,pend.premi_hadir,pend.imk,pend.uang_shift,pend.uang_jabatan,pend.tunjangan_skill,pend.tunjangan_kesehatan,pend.tunjangan_insentif,pend.uang_cuti,
//        pot.telat_1,pot.telat_2,pot.telat_3,pot.telat_4,pot.telat_5,pot.pulang_cepat_1,pot.pulang_cepat_2,pot.pulang_cepat_3');
//        $karyawan = $this->paginate(ClassRegistry::init('Karyawan'));
//        $this->set(compact('karyawan'));

        $karyawanObj = ClassRegistry::init('Karyawan');
//        debug($this->paginate($karyawanObj));

        $this->set('karyawan', $this->paginate($karyawanObj));

//        $this->set('pendapatan',$karyawan);


        ClassRegistry::flush();
    }

    public function pendapatanDanPotonganPembuatanPage() {
        $potonganBL = new PotonganBL();
        $karyawanBL = new KaryawanBL();
        $divisiBL = new DivisiBL();

        $this->set('divisi_list', $divisiBL->divisiFind('list'));
        $this->set('karyawan_list', $karyawanBL->karyawanFind('list', array('conditions' => array('Karyawan.id NOT IN(SELECT karyawan_id FROM potongan)'))));

        if (!empty($this->data)) {
            if ($potonganBL->createPotongan($this->data)) {
                $this->Session->setFlash(__('The potongan has been saved', true));
                $this->redirect(array('action' => 'potonganListDataPage'));
            } else {
                $this->Session->setFlash(__('The potongan has not been saved', true));
            }
        }
    }

    public function pendapatanDanPotonganEditPage($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid potongan', true));
            $this->redirect(array('action' => 'potonganListDataPage'));
        }

        $potonganBL = new PotonganBL();

        if (!empty($this->data)) {
            if($potonganBL->editPotongan($this->data, $id)){
                $this->Session->setFlash(__('The potongan has been saved', true));
                $this->redirect(array('action' => 'potonganListDataPage'));
            } else {
                $this->Session->setFlash(__('The potongan could not be saved. Please, try again.', true));
            }
        }

        if (empty($this->data)) {
            $this->data = $potonganBL->readDetail($id);
        }
    }

    // --- end try



    // --- GAJI AREA
    public function gajiListDataPage() {
        $this->set('gaji', $this->paginate(ClassRegistry::init('Gaji')));

        ClassRegistry::flush();
    }

    public function gajiPembuatanPage() {
        $gajiBL = new GajiBL();
        $karyawanBL = new KaryawanBL();
        $divisiBL = new DivisiBL();
        $pendapatanBL = new PendapatanBL();
        $potonganBL = new PotonganBL();

        $this->set('divisi_list', $divisiBL->divisiFind('list'));
        $this->set('karyawan_list', $karyawanBL->karyawanFind('list', array('conditions' => array('Karyawan.id NOT IN(SELECT karyawan_id FROM gaji)'))));

       
        $this->set('dataGaji', $dataGaji);

        if (!empty($this->data)) {

         $pendapatanFields = $pendapatanBL->pendapatanFind('first', array('conditions' => array('Karyawan.id = 1')));
         $this->set('pendapatan_fields', $pendapatanFields);
         $potonganFields = $potonganBL->potonganFind('first', array('conditions' => array('Karyawan.id = 1')));
         $this->set('potongan_fields', $potonganFields);

         $this->data['Gaji']['gaji_pokok'] = $pendapatanFields['Pendapatan']['gaji_pokok'];
         $this->data['Gaji']['uang_makan'] = $pendapatanFields['Pendapatan']['uang_makan'];
         $this->data['Gaji']['uang_transport'] =  $pendapatanFields['Pendapatan']['uang_transport'];
         $this->data['Gaji']['premi_hadir'] = $pendapatanFields['Pendapatan']['premi_hadir'];
         $this->data['Gaji']['imk'] = $pendapatanFields['Pendapatan']['imk'];
         $this->data['Gaji']['uang_shift'] = $pendapatanFields['Pendapatan']['uang_shift'];
         $this->data['Gaji']['uang_jabatan'] = $pendapatanFields['Pendapatan']['uang_jabatan'];
         $this->data['Gaji']['tunjangan_skill'] = $pendapatanFields['Pendapatan']['tunjangan_skill'];
         $this->data['Gaji']['tunjangan_insentif'] = $pendapatanFields['Pendapatan']['tunjangan_insentif'];
         $this->data['Gaji']['tunjangan_kesehatan'] = $pendapatanFields['Pendapatan']['tunjangan_kesehatan'];
         $this->data['Gaji']['uang_cuti'] = $pendapatanFields['Pendapatan']['uang_cuti'];

         $this->data['Gaji']['telat_1'] = $potonganFields['Potongan']['telat_1'];
         $this->data['Gaji']['telat_2'] = $potonganFields['Potongan']['telat_2'];
         $this->data['Gaji']['telat_3'] = $potonganFields['Potongan']['telat_3'];
         $this->data['Gaji']['telat_4'] = $potonganFields['Potongan']['telat_4'];
         $this->data['Gaji']['telat_5'] = $potonganFields['Potongan']['telat_5'];
         $this->data['Gaji']['pulang_cepat_1'] = $potonganFields['Potongan']['pulang_cepat_1'];
         $this->data['Gaji']['pulang_cepat_2'] = $potonganFields['Potongan']['pulang_cepat_2'];
         $this->data['Gaji']['pulang_cepat_3'] = $potonganFields['Potongan']['pulang_cepat_3'];

            if ($gajiBL->createGaji($this->data)) {
                $this->Session->setFlash(__('The potongan has been saved', true));
                $this->redirect(array('action' => 'potonganListDataPage'));
            } else {
                $this->Session->setFlash(__('The potongan has not been saved', true));
            }
            pr($this->data);
        }
    }

    public function gajiEditPage($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid gaji', true));
            $this->redirect(array('action' => 'gajiListDataPage'));
        }

        $gajiBL = new GajiBL();

        if (!empty($this->data)) {
            if($gajiBL->editGaji($this->data, $id)){
                $this->Session->setFlash(__('The gaji has been saved', true));
                $this->redirect(array('action' => 'gajiListDataPage'));
            } else {
                $this->Session->setFlash(__('The gaji could not be saved. Please, try again.', true));
            }
        }

        if (empty($this->data)) {
            $this->data = $gajiBL->readDetail($id);
        }
    }
    // --- END GAJI AREA
}
